package com.woniu.zsd.platform.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu.zsd.common.util.JWTUtil;
import com.woniu.zsd.common.util.Result;
import com.woniu.zsd.common.web.PageInfo;
import com.woniu.zsd.platform.annotation.RequirePermission;
import com.woniu.zsd.platform.annotation.RequireRole;
import com.woniu.zsd.platform.form.AddRecycleBinForm;
import com.woniu.zsd.platform.form.SelectStation;
import com.woniu.zsd.platform.form.UpdateRecycleBinForm;
import com.woniu.zsd.platform.form.UpdateSiteScoreBySiteIdForm;
import com.woniu.zsd.platform.param.AddRecycleBinParam;
import com.woniu.zsd.platform.param.SelectStationParam;
import com.woniu.zsd.platform.param.UpdateRecycleBinParam;
import com.woniu.zsd.platform.param.UpdateSiteScoreBySiteIdParam;
import com.woniu.zsd.platform.service.RecycleBinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@RestController
@Api(tags = "回收站类接口")
@RequestMapping("/recycleBin")
public class RecycleBinController {
    @Resource
    private JWTUtil jwtUtil;

    @Resource
    private RecycleBinService recycleBinService;

    /**
     * 根据站点ID查询站点名称
     * @param stationId 站点ID
     * @return 站点名称
     * @throws Exception
     */
    @GetMapping("/selectStationNameByStationId")
    @ApiOperation("根据站点ID查询站点名称")
    @RequireRole({"超级管理员"})
    @RequirePermission("根据id查询站点")
    public Result selectStationNameByStationId(@RequestParam("stationId") Integer stationId) throws Exception {
        String stationName = recycleBinService.selectStationNameByStationId(stationId);
        return Result.success("查询成功", stationName);
    }

    //更新站点积分
    @PostMapping("/updateSiteScoreBySiteId")
    @ApiOperation("根据站点ID更新站点积分")
    @RequireRole({"超级管理员"})
    @RequirePermission("更新站点积分")
    public Result updateSiteScoreBySiteId(@RequestBody UpdateSiteScoreBySiteIdForm form) throws Exception {
        recycleBinService.updateSiteScoreBySiteId(BeanUtil.toBean(form, UpdateSiteScoreBySiteIdParam.class));
        return Result.success("更新站点积分成功成功");
    }

    //根据用户经纬度查询最近的5个站点
    @GetMapping("/selectRecycleBinByLocation")
    @ApiOperation("根据用户经纬度查询最近的5个站点")
//    @RequireRole({"超级管理员","站点管理员"})
//    @RequirePermission("查询最近站点")
    public Result selectRecycleBinByLocation(@RequestParam("longitude") BigDecimal longitude, @RequestParam("latitude") BigDecimal latitude) throws Exception {
        List<Integer> staionIds = recycleBinService.selectRecycleBinByLocation(longitude, latitude);
        return Result.success("查询最近的5个站点成功",staionIds);
    }

    //新增站点
    @PostMapping("/addRecycleBin")
    @ApiOperation("新增站点")
    @RequireRole({"超级管理员"})
    @RequirePermission("新增站点")
    public Result addRecycleBin(@RequestBody AddRecycleBinForm form) throws Exception {
        recycleBinService.addRecycleBin(BeanUtil.toBean(form, AddRecycleBinParam.class));
        return Result.success("新增站点成功");
    }

    //删除站点
    @PostMapping("/deleteRecycleBin")
    @ApiOperation("删除站点")
    @RequireRole({"超级管理员"})
    @RequirePermission("删除站点")
    public Result deleteRecycleBin(Integer stationId) throws Exception {
        recycleBinService.deleteRecycleBin(stationId);
        return Result.success("删除站点成功");
    }

    //修改站点信息
    @PostMapping("/updateRecycleBin")
    @ApiOperation("修改站点信息")
    @RequireRole({"超级管理员"})
    @RequirePermission("修改站点信息")
    public Result updateRecycleBin(@RequestBody UpdateRecycleBinForm form) throws Exception {
        recycleBinService.updateRecycleBin(BeanUtil.toBean(form, UpdateRecycleBinParam.class));
        return Result.success("修改站点信息成功");
    }

    //根据accountId查询站点Id
    @GetMapping("/selectStationIdByAccountId")
    @ApiOperation("根据accountId查询站点Id")
    @RequireRole({"超级管理员"})
    @RequirePermission("根据accountId查询站点Id")
    public Result selectStationIdByAccountId(@RequestHeader("token") String token) throws Exception {
        Integer accountId = jwtUtil.getAccountId(token);
        Integer stationId = recycleBinService.selectStationIdByAccountId(accountId);
        return Result.success("查询成功", stationId);
    }

    //查询所有站点
    @GetMapping("/selectAllStation")
    @ApiOperation("查询所有站点")
    @RequireRole({"超级管理员"})
    @RequirePermission("查询所有站点")
    public Result selectAllStation(SelectStation form) throws Exception {
        PageInfo pageInfo = recycleBinService.selectAllStation(BeanUtil.toBean(form, SelectStationParam.class));
        return Result.success("查询成功", pageInfo);
    }


}

